/*
 * message.h
 * communication with clients
 * reference kbengine protocol
 * Author : macro gu
 *
 */

#pragma once
#pragma pack(1)

#define SERVER_CONF_PATH "./conf/serverinfo.xml"

#define RETURN_URL_MAX_LEN 512
#define DELETE_FILE_NAME 128
#define FDFS_GROUP_NAME_MAX_LEN 16
#define FILE_PACKAGE_MAX_LEN 1024
#define FILE_MD5_LENGTH 64

#include <cstring>
#include <stdint.h>

/*
Fixed-length types:

		 |-----------------------------------------------------------------|
Packet = |      Len: 2Bytes            |            Len: N                 |
		 |     (Message-ID: uint16)    |   (Message-Body: Binary-stream)   |
		 |-----------------------------------------------------------------|

Variable-length types:

		 |--------------------------------------------------------------------------------------------|
Packet = |      Len: 2Bytes            |       Len: 2Bytes          |           Len: N                |
		 |     (Message-ID: uint16)    |   (Message-Length: uint16) |   (Message-Body: Binary-stream) |
		 |--------------------------------------------------------------------------------------------|
*/

enum eMessageID
{
	eVMP_ReqVerifyUser = 1,
	eVMP_RtnVerifyUser = 2,
	eVMP_ReqBeginUpload = 3,
	eVMP_RtnBeginUpload = 4,
	eVMP_ReqUploadVoiceMessage = 5,			
	eVMP_RtnUploadVoiceMessage = 6,			
	eVMP_ReqDeleteUploadedFile = 7,
	eVMP_RtnDeleteUploadedFile = 8,

	eVMP_Max = 256
};

// 验证用户合法性
struct stReqVerifyUser
{
	stReqVerifyUser()
	{
		uiUserID = 0;
		uiGameID = 0;
		uiZoneID = 0;
		memset(token, 0x00, sizeof(token));
	}

	//账号id或者角色ID
	uint32_t 	uiUserID;
	//游戏id
	uint32_t 	uiGameID;
	// 区id
	uint32_t	uiZoneID;
	// token
	char token[33];
};

// 返回eVMP_ReqVerifyUser
struct stRtnVerifyUser
{
	stRtnVerifyUser(): uiRet(0) {}
	uint8_t  uiRet;		// 0:success others: failed
};

// 请求开始上传文件  每次正式开始上传前都必须要有此
struct stReqBeginToUploadFile
{
	stReqBeginToUploadFile()
	{
		uiFileSize = 0;
		memset(extensionName, 0x00, sizeof(extensionName));
	}

	//文件长度
	uint32_t	uiFileSize;
	// file extension name
	char extensionName[16];	
};

// 返回请求上传文件 消息
struct stRtnBeginToUploadFile
{
	stRtnBeginToUploadFile()
	{
		uiFlag = 0;	
	}

	uint8_t uiFlag;			// 0: success   others: failed, cannot uploadfile
};


// 请求上传语音信息
// Variable-length
// message ID is eVMP_ReqUploadVoiceMessage
// message len = voicedatalen + uiFlag
// message body first byte is flag
struct stReqUploadVoiceMessageUser 
{
	stReqUploadVoiceMessageUser():uiFlag(0)
	{
	}
	
	bool isBegin() const { return ((uiFlag & 0x01) != 0); }
	bool isEnd() const { return ((uiFlag & 0x02) != 0); }
	void setBeginFlag() { uiFlag |= 0x01; }
	void setEndFlag() { uiFlag |= 0x02; }

	uint8_t uiFlag;                //发送标志: 0000 0001-开始, 0000 0010-结束.
};

// 返回上传语音信息
// Fixed-length 
// message ID is eVMP_RtnUploadVoiceMessage
// message length = 1 + 128+1 + 16+1
// message body  Ret + url + groupname
struct stRtnUploadVoiceMessageUser 
{
	stRtnUploadVoiceMessageUser()
		: uiRet(0)
	{
		memset(cFileMd5, 0x00, sizeof(cFileMd5));
		memset(cUrldata, 0x00, sizeof(cUrldata));
		memset(cGroupname, 0x00, sizeof(cGroupname));
	}

	uint8_t uiRet;         	// 0成功 1失败	
	char cFileMd5[FILE_MD5_LENGTH];
	char cUrldata[RETURN_URL_MAX_LEN+1];		
	char cGroupname[FDFS_GROUP_NAME_MAX_LEN + 1];	// 分组的名
};

// 请求删除已经被上传的文件
// Fixed-length 
// message ID is eVMP_ReqDeleteUploadedFile 
// message length = 1 + 128+1 + 16+1
// message body  Ret + filename + groupname
struct stReqDeleteUploadedFileUserCmd
{
	stReqDeleteUploadedFileUserCmd()
	{
		memset(cFilename, 0x00, sizeof(cFilename));
		memset(cGroupname, 0x00, sizeof(cGroupname));
	}

	char cFilename[DELETE_FILE_NAME + 1];			// 需要删除的文件名
	char cGroupname[FDFS_GROUP_NAME_MAX_LEN + 1];		// 分组的名
};

// 返回请求删除已经被上传的文件
// Fixed-length 
// message ID is eVMP_RtnDeleteUploadedFile
// message length = 1
// message body  iErrorNO
struct stRtnDeleteUploadedFileUserCmd
{
	stRtnDeleteUploadedFileUserCmd()
	{
		iErrorNO = 0;
	}

	uint8_t iErrorNO;				// 0:succeed  !=0: error code
};

// fastdfs data
struct SFastdfsData 
{
	SFastdfsData()
	{
		byRet = 0;

		memset(url, 0x00, sizeof(url));
		memset(extensionName, 0x00, sizeof(extensionName));

		usedLen = 0;
		totalLen = 0;

		pVoicemessage_data = nullptr;
	}

	~SFastdfsData()
	{
		if (pVoicemessage_data != nullptr)
		{
			delete[] pVoicemessage_data;
		}
	}

	char url[256];
	char extensionName[16];

	char* pVoicemessage_data;
	uint32_t usedLen;
	uint32_t totalLen;

	uint8_t byRet;
};

#pragma pack()
